package com.costeaalex.blueserver.server;

import java.util.Calendar;


public class BlueServerClientStatus extends Thread
	{
	
	private boolean isAlive=false;
	private BlueServerReceiver bSRL;
	private long lastDate=0;
	private long currentDate=0;
	private float difference=0;
	private Calendar calendar=Calendar.getInstance();
	private BlueServerMonitor bSML;
	
	public BlueServerClientStatus(BlueServerReceiver serverR, BlueServerMonitor bSM)
		{
		super("Blue Server Status");
		bSRL=serverR;
		bSML=bSM;
		isAlive=true;
		}
	
	@Override
	public void run()
		{
		
		while(isAlive)
			{
			try
				{
				sleep(5000);
				}
			catch (InterruptedException e)
				{
				e.printStackTrace();
				}
			
			if(bSRL.isAlive())
				checkDate();
			}
		}	
	
	private void checkDate()
		{
		if(lastDate==0)
			lastDate=bSRL.getDate();
		else
			{
			lastDate=bSRL.getDate();
			calendar=Calendar.getInstance();
			currentDate=calendar.getTimeInMillis();
			difference=currentDate-lastDate;
			difference=difference/(1000*60);
			//System.out.println(lastDate+"-"+currentDate+"-"+difference);
			if(difference>=0.4)
				bSML.setClientStatus(false);
			}
		}
	
	public void destroy()
		{
		isAlive=false;
		}

	}
